<template>
  <div class="login">
    <el-form
      :model="loginUser"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="loginForm"
    >
      <el-form-item label="手机号" prop="telephone" ref="telephone">
        <el-input v-model="loginUser.telephone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPassword">
        <el-input type="userPassword" v-model="loginUser.userPassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
      <ul>
      <router-link to="/register" tag='li'><a>去注册</a></router-link> </ul>
    </el-form>
  </div>
</template>

<script>
import jwt_decode from 'jwt-decode'
export default {
  name: "login",
  data: function() {
    return {
      loginUser: {
        telephone: "",
        userPassword: ""
      },
      rules: {
        telephone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码必须是11位", trigger: "blur" }
        ],
        userPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码必须是6位", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      //通过ref定位到form表单
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert("submit!");
          this.$axios
            .post("http://localhost:3000/register/login", this.loginUser)
            .then(res => {
              // console.log(telephone)
              if(res.data.code == 200){
                alert('登录成功')
              console.log("登录成功！", res);
              console.log(res.data.token)
              console.log('token对象：',jwt_decode(res.data.token))
              localStorage.setItem('mytoken',res.data.token)  //1.把token保存到本地存储
              this.$router.push("/index/indexdown"); //路由转向主页组件
                }else{
              alert('手机号密码输入错误！请重新输入')
            }
            })
          
            .catch(err => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      //实现重置表单元素数据
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang="css" scoped>
a {
  text-decoration: none;
}
ul {
  list-style: none;
  width: 100%;
  /* background-color: #ff0; */
}
ul>li{
  width: 50px;
  /* background-color: red; */
  margin: 0 auto;
}
.login {
  width: 100%;
  height: 100%;
  background: url(../assets/login1.jpg) no-repeat center center;
  background-size: 100% 100%;
}
.loginForm {
  width: 400px;
  height: 300px;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left:150px;
  margin-top: -190px;
  border-radius: 5px;
  background-color: rgba(245, 245, 245,0.7);
  padding: 20px 50px 10px 10px;
}
/* .login ul{

} */
</style>